#!/usr/bin/env python
import pika
import time
import threading
class Inbox(threading.Thread):
    def __init__(self,queuename=''):
        threading.Thread.__init__(self)
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
        self.queueName = queuename
        self.channel = self.connection.channel()
        self.channel.queue_declare(queue=queuename, durable=True)

    @staticmethod
    def callback(ch, method, properties, body):
        print(" [x] Received %r" % body)
        time.sleep(2)
        print(" [x] Done")
        ch.basic_ack(delivery_tag=method.delivery_tag)  # send asc back

    def startListen(self):
        self.channel.basic_qos(prefetch_count=1)  # fair dispatch
        self.channel.basic_consume(Inbox.callback,queue=self.queueName)
        self.channel.start_consuming()

    def run(self):
        self.startListen()

